import { Inject, Get, Query, Post, Body } from '@midwayjs/core';
import { CoolController, BaseController } from '@cool-midway/core';
import { OrderShopEntity } from '../../entity/shop';
import { OrderShopService } from '../../service/shop';
import { OrderCityEntity } from '../../entity/city';
import { OrderMerchantEntity } from '../../entity/merchant';

/**
 * 订单模块-店铺信息
 */
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: OrderShopEntity,
  service: OrderShopService,
  pageQueryOp: {
    select: [
      'a.*',
      'b.cityName as cityName',
      'c.merchantName as agentMerchantName',
      'd.merchantName as payMerchantName',
    ],
    keyWordLikeFields: ['a.name', 'a.address'],
    join: [
      {
        entity: OrderCityEntity,
        alias: 'b',
        condition: 'a.cityId = b.id',
        type: 'leftJoin',
      },
      {
        entity: OrderMerchantEntity,
        alias: 'c',
        condition: 'a.agentMerchantId = c.id',
        type: 'leftJoin',
      },
      {
        entity: OrderMerchantEntity,
        alias: 'd',
        condition: 'a.payMerchantId = d.id',
        type: 'leftJoin',
      },
    ],
  },
  
})
export class AdminOrderShopController extends BaseController {
  @Inject()
  orderShopService: OrderShopService;

  @Get('/getSelectList')
  async getSelectList() {
    return this.ok(await this.orderShopService.getSelectList());
  }

  @Get('/getShopSelectList')
  async getShopSelectList(@Query('shopId') shopId: number) {
    return this.ok(await this.orderShopService.getShopSelectList(shopId));
  }

  // 根据订单ID获取店铺信息
  @Get('/getShopInfoByOrderId')
  async getShopInfoByOrderId(@Query('orderId') orderId: number) {
    return this.ok(await this.orderShopService.getShopInfoByOrderId(orderId));
  }

  // 根据用户id获取商铺信息
  @Post('/getShopInfoByUserId')
  async getShopInfoByUserId(@Body('userId') body: {}) {
    return this.ok(await this.orderShopService.getShopInfoByUserId(body));
  }

  // 根据代理商ID获取店铺
  @Get('/getShopSelectListByAgentId')
  async getShopSelectListByAgentId(@Query('agentId') agentId: number) {
    return this.ok(await this.orderShopService.getShopSelectListByAgentId(agentId));
  }
}
